package com.ftg.learn.chapter04.dao;

import com.ftg.learn.chapter04.domain.BedRoom;

import java.math.BigInteger;
import java.util.Arrays;

/**
 * 寝室数据访问类
 *
 * @author 布衣
 */
public class BedRoomDao {

    //面向对象三大特征   ： 封装(良好封装)  继承  多态

    //剩余床位
    private int restNum;
    //寝室列表
    private BedRoom[] allRooms;
    int default_size = 10;

    private int size;

    public BedRoomDao() {
        allRooms = new BedRoom[default_size];
    }

    public int getRestNum() {
        return restNum;
    }

    public void setRestNum(int restNum) {
        this.restNum = restNum;
    }

    /**
     * 获取所有有效寝室数量
     *
     * @return
     */
    public BedRoom[] getAllRooms() {
        return Arrays.copyOf(allRooms, size);
    }

    /**
     * 有效的寝室数量
     *
     * @return int
     */
    public int size() {
        return this.size;
    }

    /**
     * 两人间
     */
    public BedRoom createBedRoom(int humNum, int firmNum) {
        return initBedRoom(null, null, humNum, firmNum);
    }

    public boolean saveBedRoom(String doorNum, String address, int humNum, int firmNum) {
        initBedRoom(doorNum, address, humNum, firmNum);
        return true;
    }

    private BedRoom initBedRoom(String doorNum, String address, int humNum, int firmNum) {
        BedRoom bed1 = new BedRoom(humNum, firmNum);
        bed1.setDoorNum(doorNum);
        bed1.setAddress(address);

        if (this.size == this.allRooms.length) {
            allRooms = Arrays.copyOf(allRooms, allRooms.length + (allRooms.length >> 1));
        }
        allRooms[size++] = bed1;
        this.restNum+=humNum;
        return bed1;
    }


}
